package com.example.frenchconjugationquiz.data;

import common.Utility;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class ConjugationDBHelper extends SQLiteOpenHelper
{
	public static final String TABLE_CONJUGATION = "conjugation";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_SUBJECT = "subject";
	public static final String COLUMN_TENSE = "tense";
	public static final String COLUMN_VERB = "verb";
	public static final String COLUMN_CONJUGATION = "conjugation";

	private static final String DATABASE_NAME = "conjugation.db";
	private static final int DATABASE_VERSION = 1;

	// Database creation sql statement
	private static final String DATABASE_CREATE = "create table " + TABLE_CONJUGATION + "(" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_SUBJECT + " text not null , " + COLUMN_VERB + " text not null , " + COLUMN_TENSE + " text not null , " + COLUMN_CONJUGATION + " text not null, UNIQUE (" + COLUMN_SUBJECT + "," + COLUMN_TENSE + "," + COLUMN_VERB + "," + COLUMN_CONJUGATION + ") ON CONFLICT REPLACE);";

	public ConjugationDBHelper(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database)
	{
		Utility.Log("ConjugationDBHelper creating table");
		database.execSQL(DATABASE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		Utility.Log("Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONJUGATION);
		onCreate(db);
	}

	public void resetDb(SQLiteDatabase db)
	{
		Utility.Log("Resetting database which will destroy all data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONJUGATION);
		onCreate(db);

	}

}